import os
import random
import xml.etree.ElementTree as ET

from utils.utils import get_classes
import shutil
    



def rename(xmlpath,jpgpath,xmloutpath,jpgoutpath,startindex=0,prefix='',zfillnum=6):
    if not os.path.exists(xmloutpath):
        os.mkdir(xmloutpath)
    if not os.path.exists(jpgoutpath):
        os.mkdir(jpgoutpath)    
    jpgfiles = os.listdir(jpgpath)
    i = startindex
    jpgfiles.sort()
    for fileName in jpgfiles:
        srcJpgFile = os.path.join(jpgfilepath,fileName)
        dstJpgFile = os.path.join(jpgFileOutPath,prefix+'_'+str(i).zfill(zfillnum)+".jpg")
        shutil.copyfile(srcJpgFile,dstJpgFile)
        print(dstJpgFile)
        srcXmlfile = os.path.join(xmlpath,os.path.splitext(fileName)[0]+".xml")
        dstXmlFile = os.path.join(xmloutpath,prefix+'_'+str(i).zfill(zfillnum)+".xml")
        if os.path.exists(srcXmlfile):
            shutil.copyfile(srcXmlfile,dstXmlFile)
            print(dstXmlFile)
        else:
            print("not found file: ",srcXmlfile)
        i += 1


VOCdevkit_path = 'digger'
if __name__ == "__main__":
    xmlfilepath = os.path.join(VOCdevkit_path, 'Annotations')
    jpgfilepath = os.path.join(VOCdevkit_path, 'JPEGImages')
    xmlFileOutPath = os.path.join(VOCdevkit_path, 'Annotations_out')
    jpgFileOutPath = os.path.join(VOCdevkit_path, 'JPEGImages_out')
    rename(xmlfilepath,jpgfilepath,xmlFileOutPath,jpgFileOutPath,1,'digger')
